package zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.technicalparameter.sysconfig.SysConfigInfo;
import zzs.generalframework.common.core.parameter.technicalparameter.sysconfig.SysConfigPO;
import zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.dao.ISysConfigDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.technicalparameter.sysconfig.dao.impl.sysConfigDaoImpl")
public class SysConfigDaoImpl implements ISysConfigDao
{
	private Logger logger = LoggerFactory.getLogger(SysConfigDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(SysConfigPO sysConfigPO)
	{
		logger.info("====插入系统配置数据开始====");
		logger.debug("====插入系统配置数据,入参:SysConfigPO=[{}]====", sysConfigPO);
		entityManager.persist(sysConfigPO);
		logger.debug("====插入系统配置数据,出参:boolean=[{}]====", true);
		logger.info("====插入系统配置数据结束====");
		return true;
	}

	@Override
	public boolean update(SysConfigPO sysConfigPO)
	{
		logger.info("====更新系统配置数据开始====");
		logger.debug("====更新系统配置数据,入参:SysConfigPO=[{}]====", sysConfigPO);
		entityManager.merge(sysConfigPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新系统配置数据结束====");
		return true;
	}

	@Override
	public List<SysConfigPO> select(SysConfigInfo sysConfigInfo)
	{
		logger.info("====查询系统配置数据开始====");
		logger.debug("====查询系统配置数据,入参:SysConfigInfo=[{}]====", sysConfigInfo);
		List<SysConfigPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from SysConfigPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getSysConfigID()))
		{
			sqlBuffer.append(" and a.sysConfigID=?");
			parameter.putParameterObject(sysConfigInfo.getSysConfigID());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getSysConfigCode()))
		{
			sqlBuffer.append(" and a.sysConfigCode=?");
			parameter.putParameterObject(sysConfigInfo.getSysConfigCode());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getSysConfigName()))
		{
			sqlBuffer.append(" and a.sysConfigName=?");
			parameter.putParameterObject(sysConfigInfo.getSysConfigName());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getSysConfigValue()))
		{
			sqlBuffer.append(" and a.sysConfigValue=?");
			parameter.putParameterObject(sysConfigInfo.getSysConfigValue());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getExtendedUsage4()))
		{
			sqlBuffer.append(" and a.extendedUsage4=?");
			parameter.putParameterObject(sysConfigInfo.getExtendedUsage4());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getExtendedUsage3()))
		{
			sqlBuffer.append(" and a.extendedUsage3=?");
			parameter.putParameterObject(sysConfigInfo.getExtendedUsage3());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getExtendedUsage2()))
		{
			sqlBuffer.append(" and a.extendedUsage2=?");
			parameter.putParameterObject(sysConfigInfo.getExtendedUsage2());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getExtendedUsage1()))
		{
			sqlBuffer.append(" and a.extendedUsage1=?");
			parameter.putParameterObject(sysConfigInfo.getExtendedUsage1());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(sysConfigInfo.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(sysConfigInfo.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(sysConfigInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询系统配置数据,出参:List<SysConfigPO>=[{}]====", returnList);
		logger.info("====查询系统配置数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(SysConfigPO sysConfigPO)
	{
		logger.info("====删除系统配置数据开始====");
		logger.debug("====删除系统配置数据,入参:SysConfigPO=[{}]====", sysConfigPO);
		entityManager.remove(sysConfigPO);
		logger.debug("====删除系统配置数据,出参:boolean=[{}]====", true);
		logger.info("====删除系统配置数据结束====");
		return true;
	}

	@Override
	public Long count(SysConfigPO sysConfigPO)
	{
		logger.info("====统计系统配置数据开始====");
		logger.debug("====统计系统配置数据,入参:SysConfigPO=[{}]====", sysConfigPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from SysConfigPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getSysConfigID()))
		{
			sqlBuffer.append(" and a.sysConfigID=?");
			parameter.putParameterObject(sysConfigPO.getSysConfigID());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getSysConfigCode()))
		{
			sqlBuffer.append(" and a.sysConfigCode=?");
			parameter.putParameterObject(sysConfigPO.getSysConfigCode());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getSysConfigName()))
		{
			sqlBuffer.append(" and a.sysConfigName=?");
			parameter.putParameterObject(sysConfigPO.getSysConfigName());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getSysConfigValue()))
		{
			sqlBuffer.append(" and a.sysConfigValue=?");
			parameter.putParameterObject(sysConfigPO.getSysConfigValue());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getExtendedUsage4()))
		{
			sqlBuffer.append(" and a.extendedUsage4=?");
			parameter.putParameterObject(sysConfigPO.getExtendedUsage4());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getExtendedUsage3()))
		{
			sqlBuffer.append(" and a.extendedUsage3=?");
			parameter.putParameterObject(sysConfigPO.getExtendedUsage3());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getExtendedUsage2()))
		{
			sqlBuffer.append(" and a.extendedUsage2=?");
			parameter.putParameterObject(sysConfigPO.getExtendedUsage2());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getExtendedUsage1()))
		{
			sqlBuffer.append(" and a.extendedUsage1=?");
			parameter.putParameterObject(sysConfigPO.getExtendedUsage1());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(sysConfigPO.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(sysConfigPO.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(sysConfigPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(sysConfigPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计系统配置数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计系统配置数据结束====");
		return returnLong;
	}

}
